package cn.edu.scau.sec.rw.view;

import cn.edu.scau.sec.rw.controllers.CommonBridge;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Paint;
import javafx.stage.Modality;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class File_DiskUI {

    double x = 0;
    double y = 0;

    public File_DiskUI(){
        CommonBridge.stage = new Stage();
        initStage(CommonBridge.stage, "/cn/edu/scau/sec/rw/view/fxml/Disk_FileUI.fxml", "cn/edu/scau/sec/rw/view/css/css.css");
    }

    private void initStage(Stage _stage, String fxml, String css){
        Stage stage = _stage;
        try {
            Parent root = FXMLLoader.load(getClass().getResource(fxml));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(css);
            scene.setFill(Paint.valueOf("#00000000"));
//            URL url =getClass().getClassLoader().getResource("icon/鼠标.png");
//            String path = url.toExternalForm();
//            scene.setCursor(Cursor.cursor(path));
            stage.setScene(scene);
            stage.setTitle("PhotoManager");
            stage.initStyle(StageStyle.TRANSPARENT);
            //Initialize the Stage with type of modal
            stage.initModality(Modality.APPLICATION_MODAL);
            //Set the owner of the Stage
            stage.initOwner(CommonBridge.primaryStage);
            stage.show();
            stage.getIcons().add(new Image("/cn/edu/scau/sec/rw/view/img/磁盘.png"));
            scene.setOnMousePressed(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                    // TODO 自动生成的方法存根
                    if(event.getButton().name().equals(MouseButton.PRIMARY.name())){
                        x = event.getScreenX()-stage.getX();
                        y = event.getScreenY()-stage.getY();
                    }
                }
            });

            scene.setOnMouseDragged(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                    // TODO 自动生成的方法存根
                    if(event.getButton().name().equals(MouseButton.PRIMARY.name())&&(event.getScreenX()<=
                            Screen.getPrimary().getVisualBounds().getMaxX()||event.getScreenY()<=Screen.getPrimary().getVisualBounds().getMaxY())){
                        if(event.getScreenX()<=Screen.getPrimary().getVisualBounds().getMaxX()) stage.setX(event.getScreenX()-x);
                        if(event.getScreenY()<=Screen.getPrimary().getVisualBounds().getMaxY()) stage.setY(event.getScreenY()-y);
                    }
                }
            });
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
